#include "Rectangles.h"
#include "Define.h"

Rectangles::Rectangles(void) : Object()
{
	this->m_iRectWidth = 1;
	this->m_iRectHeight = 1;
	SetType(RECTANGLE);
}

Rectangles::Rectangles(float posX, float posY, int width, int height) : Object(posX, posY)
{
	this->m_iRectWidth = width;
	this->m_iRectHeight = height;
	SetType(RECTANGLE);
}

Rectangles::~Rectangles()
{

}

void Rectangles::Update(float frameTime)
{
	Object::Update(frameTime);

	int hasCollide = IsCollideEdge();
	if (CheckFlag(hasCollide, BOTTOM))
	{
		SetPosition(m_iPositionX, (float)(SCREEN_H - m_iRectHeight));
	} else if (CheckFlag(hasCollide, TOP))
	{
		SetPosition(m_iPositionX, 0);
	}

	if (CheckFlag(hasCollide, RIGHT))
	{
		SetPosition((float)(SCREEN_W - m_iRectWidth), m_iPositionY);
	} else if (CheckFlag(hasCollide, LEFT))
	{
		SetPosition(0, m_iPositionY);
	}
}

void Rectangles::Render()
{
	VideoDriver::GetInstance()->DrawRect(
		(int)this->m_iPositionX,
		(int)this->m_iPositionY,
		this->m_iRectWidth,
		this->m_iRectHeight);

}

void Rectangles::SetWidth(int width)
{
	this->m_iRectWidth = width;
}

void Rectangles::SetHeight(int height)
{
	this->m_iRectHeight = height;
}

bool Rectangles::IsInRect(int x, int y)
{
	if (x > this->m_iPositionX 
		&& x < this->m_iPositionX + this->m_iRectWidth
		&& y > this->m_iPositionY
		&& y < this->m_iPositionY + this->m_iRectHeight)
	{
		return true;
	}
		
	return false;
}

void Rectangles::HandleTouchAction(Touch *touch)
{
	if (touch->touchState == StateMove)
	{
		/*int translateX = touch->currPosX - touch->prevPosX;
		int translateY = touch->currPosY - touch->prevPosY;
		MoveBy(translateX, translateY);*/
	}
}

bool Rectangles::IsCollideWithRect(float x, float y, int width, int height)
{
	//RECT with RECT
	bool result = false;
	if (this->m_iPositionX + this->m_iRectWidth < x
		|| this->m_iPositionY + this->m_iRectHeight < y
		|| this->m_iPositionX > x + width
		|| this->m_iPositionY > y + height)
	{
		result = false;
	} else {
		result = true;
	}
	return result;
}

bool Rectangles::IsCollideWithObject(Object* object)
{
	int objType = object->GetType();
	if (objType == CIRCLE)
	{
		return IsCollideWithCircle((Circle*)object);
	}
	if (objType == RECTANGLE)
	{
		return ((Rectangles*)object)->IsCollideWithRect(
			m_iPositionX, m_iPositionY, m_iRectWidth, m_iRectHeight);
	}

	printf("ERROR!! Can't check collision RECT with object type %d\n", objType);
	return false;
}

bool Rectangles::IsCollideWithCircle(Circle* circle)
{
	return circle->IsCollideWithRect(
		m_iPositionX, m_iPositionY, m_iRectWidth, m_iRectHeight);
}

int Rectangles::GetWidth()
{
	return m_iRectWidth;
}
int Rectangles::GetHeight()
{
	return m_iRectHeight;
}

int Rectangles::IsCollideEdge()
{
	int result = 0;
	if (m_iPositionX <= 0)
	{
		result |= LEFT;
		//return LEFT;
		
	} else if (m_iPositionX >= (int)SCREEN_W - m_iRectWidth)
	{
		result |= RIGHT;	
		//return RIGHT;
		
	}
	
	if (m_iPositionY <= 0)
	{
		result |= TOP;
		//return TOP;
	} else if (m_iPositionY >= (int)SCREEN_H - m_iRectHeight)
	{
		result |= BOTTOM;
		//return BOTTOM;
	}
	return result;
}

float Rectangles::GetCenterPosX()
{
	return m_iPositionX + m_iRectWidth/2;
}

float Rectangles::GetCenterPosY()
{
	return m_iPositionY + m_iRectHeight/2;
}